package com.wanchuang.shengfubao.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.wanchuang.shengfubao.R;
import com.wanchuang.shengfubao.adapter.PolicySelectAdapter;
import com.wanchuang.shengfubao.adapter.PolicySelectBean;
import com.wanchuang.shengfubao.view.MaxHeightRecyclerView;
import java.util.List;
import kale.ui.view.dialog.BaseCustomDialog;
import kale.ui.view.dialog.BaseEasyDialog;
import kale.ui.view.dialog.EasyDialog;

/**
 * Created by AndroidIntexh1 on 2018/9/25.
 */
public class PolicyDialog extends BaseCustomDialog {

    public static final String KEY_TYPE = "KEY_TYPE";

    public PolicyDialog.OnDialogClickListener dialogListener;


    private MaxHeightRecyclerView recyclerView;
    private PolicySelectAdapter mAdapter;
    public List<PolicySelectBean> snData;

    private LinearLayoutManager linearLayoutManager;


    /**
     * 继承自{@link EasyDialog.Builder}以扩展builder
     */
    public static class Builder extends BaseEasyDialog.Builder<Builder> {

        private PolicyDialog.OnDialogClickListener listener;
        private List<PolicySelectBean> datas;
        private Bundle bundle = new Bundle();

        public Builder(@NonNull Context context) {
            super(context);
        }


        public Builder setType(String data) {
            bundle.putString(KEY_TYPE, data);
            return this;
        }


        public PolicyDialog.Builder setOnDialogClickListener(PolicyDialog.OnDialogClickListener lis) {
            listener = lis;
            return this;
        }

        public PolicyDialog.Builder setData(List<PolicySelectBean> data) {
            datas = data;
            return this;
        }


        @NonNull
        @Override
        protected EasyDialog createDialog() {
            PolicyDialog dialog = new PolicyDialog();
            dialog.setArguments(bundle); // 增加自己的bundle
            dialog.dialogListener = listener;
            dialog.snData = datas;
            return dialog;
        }
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_recycle_policy;
    }

    @Override
    protected void bindViews(View view) {
        recyclerView = findView(R.id.max_recyclerView);
        Button btn_sure = findView(R.id.tv_sure);
        initRecyclerView();
        mAdapter.addAll(snData);
        btn_sure.setOnClickListener(view12 -> {
            dialogListener.onClick(view12,0);
            dismiss();
        });

    }

    @Override
    protected void setViews() {
        setBackground();
        setLayout();

    }

    /**
     * 强制取消背景，保持有透明
     */
    private void setBackground() {
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable()); // 去除dialog的背景，即透明
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0xffffffff)); // 设置白色背景
//        getDialog().getWindow().setBackgroundDrawableResource(R.drawable.dialog_shop_jion_bg); // 设置背景
    }


    /**
     * 也可通过setLayout来设置：
     * getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
     */
    private void setLayout() {
        Window window = getDialog().getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();

        // 强制宽高
//        int padding = getResources().getDimensionPixelOffset(R.dimen.dialog_padding);
        lp.width = getScreenWidth(getActivity());
//        lp.height = getResources().getDimensionPixelOffset(R.dimen.dialog_height);

        lp.gravity = Gravity.BOTTOM; // 设置展示的位置
        window.setAttributes(lp);
    }

    @Override
    protected void modifyAlertDialogBuilder(AlertDialog.Builder builder) {
        super.modifyAlertDialogBuilder(builder);
    }

    public static int getScreenWidth(Activity activity) {
        final DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }


    /**
     * 监听输入框内容
     */
    public interface OnDialogClickListener {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        void onClick(View v, int positon);
    }

    private void initRecyclerView() {
        linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(mAdapter = new PolicySelectAdapter(getContext()));
        mAdapter.setOnItemClickListener(position -> {
            snData.get(position).setSelect(!snData.get(position).isSelect());
            mAdapter.notifyItemChanged(position);
        });
    }


}
